﻿using AEFunction;
using CommonFunction;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace AETools
{

    public partial class SelectNewShapeFields : Form
    {

        public ILayer[] Layers;
        public string[] ExcelFields;
        public string [] ShapeSelectedFields;
        public List<ExcelField> ExcelSelectedFields;
        public string SavePath;
        public SelectNewShapeFields(ILayer[] layers, string[] excelFields)
        {
            InitializeComponent();
            ExcelSelectedFields = new List<ExcelField>();
            Layers = layers;
            ExcelFields = excelFields;
            //comboBox1.Items.Clear();
            foreach (ILayer l in layers)
            {
                comboBox1.Items.Add((object)l.Name);
            }
            comboBox1.SelectedItem = comboBox1.Items[0];
            //listBox1.Items.Clear();

            //listBox2.Items.Clear();
            foreach (string s in excelFields)
            {
                listBox4.Items.Add((object)s);
            }

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            foreach (ILayer l in Layers)
            {
                if (l.Name == comboBox1.SelectedItem.ToString())
                {

                    IFeatureClass featureClass = ArcEngineFunction.GetLayerFeatureClass(l);
                    IFields layerFields = featureClass.Fields;
                    for (int i = 0; i < layerFields.FieldCount; i++)
                    {
                        IField field = layerFields.get_Field(i);
                        listBox1.Items.Add(field.Name);
                        listBox1.SelectedIndex = 0;
                    }
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            listBox2.Items.AddRange(listBox1.Items);
            listBox3.Items.AddRange(listBox4.Items);
            listBox1.Items.Clear();
            listBox4.Items.Clear();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            listBox1.Items.AddRange(listBox2.Items);
            listBox4.Items.AddRange(listBox3.Items);
            listBox2.Items.Clear();
            listBox3.Items.Clear();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            List<object> selectedItems = new List<object>();
            
            selectedItems = Function.GetSelectItems(listBox1);
            listBox2.Items.AddRange(selectedItems.ToArray());
            Function.RemoveSelectItems(listBox1, selectedItems);

            selectedItems.Clear();
            selectedItems = Function.GetSelectItems(listBox4);
            listBox3.Items.AddRange(selectedItems.ToArray());
            Function.RemoveSelectItems(listBox4, selectedItems);
            selectedItems.Clear();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            List<object> selectedItems = new List<object>();
            selectedItems = Function.GetSelectItems(listBox2);
            listBox1.Items.AddRange(selectedItems.ToArray());
            Function.RemoveSelectItems(listBox2, selectedItems);

            selectedItems.Clear();
            selectedItems = Function.GetSelectItems(listBox3);
            listBox4.Items.AddRange(selectedItems.ToArray());
            Function.RemoveSelectItems(listBox3, selectedItems);
            selectedItems.Clear();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("未选择文件保存路径！!！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (listBox2.Items.Count + listBox3.Items.Count > 26)
            {
                MessageBox.Show("字段数量过多，请重新选择！!！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            ShapeSelectedFields=new string[listBox2.Items.Count];
            for (int i = 0; i < listBox2.Items.Count; i++)
            {
                ShapeSelectedFields[i] = listBox2.Items[i].ToString();
            }
            for (int i = 0; i < listBox3.Items.Count; i++)
            {
                ExcelField ef = new ExcelField();
                ef.field = listBox3.Items[i].ToString();
                ef.type = Type.GetType("System.String");
                ExcelSelectedFields.Add(ef);
            }
            this.DialogResult = DialogResult.OK;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "ShapeFile(*.shp)|*.shp";
            sfd.Title = "选择shp文件保存路径";
            if (sfd.ShowDialog() != DialogResult.OK)
                return;
            textBox1.Text=sfd.FileName;
            SavePath = textBox1.Text;
        }
    }
}
